home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / GIFLIB12.ARJ / GIFHISTO.C < prev    next >
C/C++ Source or Header  |  1991-05-12  |  9KB  |  265 lines

  1. /*****************************************************************************
  2. *   "Gif-Lib" - Yet another gif library.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 0.1, Jul. 1989   *
  5. ******************************************************************************
  6. * Program to create histogram of the colors used by the given GIF file.      *
  7. * Dumps out GIF file of constants size GIF_WIDTH by GIF_HEIGHT.             *
  8. * Options:                                     *
  9. * -q : quite printing mode.                             *
  10. * -t : Dump out text instead of GIF - #Colors lines, each with #appearances. *
  11. * -i W H : size of GIF image to generate. Colors of input GIF file are         *
  12. *      spread homogeneously along Height, which better by dividable by the   *
  13. *      number of colors in input image.                         *
  14. * -n n : select image number to generate histogram to (1 by default).         *
  15. * -b : strip off background color count.                     *
  16. * -h : on line help                                 *
  17. ******************************************************************************
  18. * History:                                     *
  19. * 8 Jul 89 - Version 1.0 by Gershon Elber.                     *
  20. *****************************************************************************/
  21.  
  22. #ifdef __MSDOS__
  23. #include <stdlib.h>
  24. #include <alloc.h>
  25. #endif /* __MSDOS__ */
  26.  
  27. #include <stdio.h>
  28. #include <ctype.h>
  29. #include <string.h>
  30. #include "gif_lib.h"
  31. #include "getarg.h"
  32.  
  33. #define PROGRAM_NAME    "GifHisto"
  34.  
  35. #define DEFAULT_HISTO_WIDTH    100          /* Histogram image diemnsions. */
  36. #define DEFAULT_HISTO_HEIGHT    256
  37. #define HISTO_BITS_PER_PIXEL    2    /* Size of bitmap for histogram GIF. */
  38.  
  39. #ifdef __MSDOS__
  40. extern unsigned int
  41.     _stklen = 16384;                 /* Increase default stack size. */
  42. #endif /* __MSDOS__ */
  43.  
  44. #ifdef SYSV
  45. static char *VersionStr =
  46.         "Gif library module,\t\tGershon Elber\n\
  47.     (C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  48. static char
  49.     *CtrlStr = "GifHisto q%- t%- s%-Width|Height!d!d n%-ImageNumber!d b%- h%- GifFile!*s";
  50. #else
  51. static char
  52.     *VersionStr =
  53.     PROGRAM_NAME
  54.     GIF_LIB_VERSION
  55.     "    Gershon Elber,    "
  56.     __DATE__ ",   " __TIME__ "\n"
  57.     "(C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  58. static char
  59.     *CtrlStr =
  60.     PROGRAM_NAME
  61.     " q%- t%- s%-Width|Height!d!d n%-ImageNumber!d b%- h%- GifFile!*s";
  62. #endif /* SYSV */
  63.  
  64. static int
  65.     ImageWidth = DEFAULT_HISTO_WIDTH,
  66.     ImageHeight = DEFAULT_HISTO_HEIGHT,
  67.     ImageN = 1;
  68. static GifColorType
  69.     HistoColorMap[] = {             /* Constant bit map for histograms: */
  70.     { 0, 0, 0 },
  71.     { 255,   0,   0 },
  72.     {   0, 255,   0 },
  73.     {   0,   0, 255 }
  74.     };
  75.  
  76. static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut);
  77.  
  78. /******************************************************************************
  79. * Interpret the command line and scan the given GIF file.              *
  80. ******************************************************************************/
  81. void main(int argc, char **argv)
  82. {
  83.     int    i, j, Size, Error, NumFiles, ExtCode, CodeSize, NumColors = 2, Color,
  84.     Count, ImageNum = 0, TextFlag = FALSE, SizeFlag = FALSE,
  85.     ImageNFlag = FALSE, BackGroundFlag = FALSE, HelpFlag = FALSE;
  86.     long Scaler, Histogram[256];
  87.     GifRecordType RecordType;
  88.     GifByteType *Extension, *CodeBlock;
  89.     char **FileName = NULL;
  90.     GifRowType Line;
  91.     GifFileType *GifFileIn = NULL, *GifFileOut = NULL;
  92.  
  93.     /* Same image dimension vars for both Image & ImageN as only one allowed */
  94.     if ((Error = GAGetArgs(argc, argv, CtrlStr, &GifQuitePrint,
  95.         &TextFlag, &SizeFlag, &ImageWidth, &ImageHeight,
  96.         &ImageNFlag, &ImageN, &BackGroundFlag,
  97.         &HelpFlag, &NumFiles, &FileName)) != FALSE ||
  98.         (NumFiles > 1 && !HelpFlag)) {
  99.     if (Error)
  100.         GAPrintErrMsg(Error);
  101.     else if (NumFiles > 1)
  102.         GIF_MESSAGE("Error in command line parsing - one GIF file please.");
  103.     GAPrintHowTo(CtrlStr);
  104.     exit(1);
  105.     }
  106.  
  107.     if (HelpFlag) {
  108.     fprintf(stderr, VersionStr);
  109.     GAPrintHowTo(CtrlStr);
  110.     exit(0);
  111.     }
  112.  
  113.     if (NumFiles == 1) {
  114.     if ((GifFileIn = DGifOpenFileName(*FileName)) == NULL)
  115.         QuitGifError(GifFileIn, GifFileOut);
  116.     }
  117.     else {
  118.     /* Use the stdin instead: */
  119.     if ((GifFileIn = DGifOpenFileHandle(0)) == NULL)
  120.         QuitGifError(GifFileIn, GifFileOut);
  121.     }
  122.  
  123.     for (i = 0; i < 256; i++) Histogram[i] = 0;          /* Reset counters. */
  124.  
  125.     /* Scan the content of the GIF file and load the image(s) in: */
  126.     do {
  127.     if (DGifGetRecordType(GifFileIn, &RecordType) == GIF_ERROR)
  128.         QuitGifError(GifFileIn, GifFileOut);
  129.  
  130.     switch (RecordType) {
  131.         case IMAGE_DESC_RECORD_TYPE:
  132.         if (DGifGetImageDesc(GifFileIn) == GIF_ERROR)
  133.             QuitGifError(GifFileIn, GifFileOut);
  134.  
  135.         if (GifFileIn -> IColorMap)
  136.             NumColors = (1 << GifFileIn -> IBitsPerPixel);
  137.         else if (GifFileIn -> SColorMap)
  138.             NumColors = (1 << GifFileIn -> SBitsPerPixel);
  139.         else
  140.             GIF_EXIT("Neither Screen nor Image color map exists.");
  141.  
  142.         if ((ImageHeight / NumColors) * NumColors != ImageHeight)
  143.             GIF_EXIT("Image height specified not dividable by #colors.");
  144.  
  145.         if (++ImageNum == ImageN) {
  146.             /* This is the image we should make histogram for:       */
  147.             Line = (GifRowType) malloc(GifFileIn -> IWidth *
  148.                             sizeof(GifPixelType));
  149.             GifQprintf("\n%s: Image %d at (%d, %d) [%dx%d]:     ",
  150.             PROGRAM_NAME, ImageNum,
  151.             GifFileIn -> ILeft, GifFileIn -> ITop,
  152.             GifFileIn -> IWidth, GifFileIn -> IHeight);
  153.  
  154.             for (i = 0; i < GifFileIn -> IHeight; i++) {
  155.             if (DGifGetLine(GifFileIn, Line, GifFileIn -> IWidth)
  156.                 == GIF_ERROR)
  157.                 QuitGifError(GifFileIn, GifFileOut);
  158.             for (j = 0; j < GifFileIn -> IWidth; j++)
  159.                 Histogram[Line[j]]++;
  160.             GifQprintf("\b\b\b\b%-4d", i);
  161.             }
  162.  
  163.             free((char *) Line);
  164.         }
  165.         else {
  166.             /* Skip the image: */
  167.             /* Now read image itself in decoded form as we dont      */
  168.             /* really care what is there, and this is much faster.   */
  169.             if (DGifGetCode(GifFileIn, &CodeSize, &CodeBlock) == GIF_ERROR)
  170.             QuitGifError(GifFileIn, GifFileOut);
  171.             while (CodeBlock != NULL)
  172.             if (DGifGetCodeNext(GifFileIn, &CodeBlock) == GIF_ERROR)
  173.                 QuitGifError(GifFileIn, GifFileOut);
  174.         }
  175.         break;
  176.         case EXTENSION_RECORD_TYPE:
  177.         /* Skip any extension blocks in file: */
  178.         if (DGifGetExtension(GifFileIn, &ExtCode, &Extension) == GIF_ERROR)
  179.             QuitGifError(GifFileIn, GifFileOut);
  180.  
  181.         while (Extension != NULL) {
  182.             if (DGifGetExtensionNext(GifFileIn, &Extension) == GIF_ERROR)
  183.             QuitGifError(GifFileIn, GifFileOut);
  184.         }
  185.         break;
  186.         case TERMINATE_RECORD_TYPE:
  187.         break;
  188.         default:            /* Should be traps by DGifGetRecordType. */
  189.         break;
  190.     }
  191.     }
  192.     while (RecordType != TERMINATE_RECORD_TYPE);
  193.  
  194.     /* We we requested to kill back ground count: */
  195.     if (BackGroundFlag) Histogram[GifFileIn -> SBackGroundColor] = 0;
  196.  
  197.     if (DGifCloseFile(GifFileIn) == GIF_ERROR)
  198.     QuitGifError(GifFileIn, GifFileOut);
  199.  
  200.  
  201.     /* We may required to dump out the histogram as text file: */
  202.     if (TextFlag) {
  203.     for (i = 0; i < NumColors; i++)
  204.         printf("%12ld  %3d\n", Histogram[i], i);
  205.     }
  206.     else {
  207.     /* Open stdout for the histogram output file: */
  208.     if ((GifFileOut = EGifOpenFileHandle(1)) == NULL)
  209.         QuitGifError(GifFileIn, GifFileOut);
  210.  
  211.     /* Dump out screen descriptor to fit histogram dimensions: */
  212.     if (EGifPutScreenDesc(GifFileOut,
  213.         ImageWidth, ImageHeight, HISTO_BITS_PER_PIXEL, 0,
  214.         HISTO_BITS_PER_PIXEL, HistoColorMap) == GIF_ERROR)
  215.         QuitGifError(GifFileIn, GifFileOut);
  216.  
  217.     /* Dump out image descriptor to fit histogram dimensions: */
  218.     if (EGifPutImageDesc(GifFileOut,
  219.         0, 0, ImageWidth, ImageHeight,
  220.         FALSE, HISTO_BITS_PER_PIXEL, NULL) == GIF_ERROR)
  221.         QuitGifError(GifFileIn, GifFileOut);
  222.  
  223.     /* Prepare scan line for histogram file, and find scaler to scale    */
  224.     /* histogram to be between 0 and ImageWidth:                 */
  225.     Line = (GifRowType) malloc(ImageWidth * sizeof(GifPixelType));
  226.     for (Scaler = 0, i = 0; i < NumColors; i++) if (Histogram[i] > Scaler)
  227.         Scaler = Histogram[i];
  228.     Scaler /= ImageWidth;
  229.     if (Scaler == 0) Scaler = 1;  /* In case maximum is less than width. */
  230.  
  231.     /* Dump out the image itself: */
  232.     for (Count = ImageHeight, i = 0, Color = 1; i < NumColors; i++) {
  233.         if ((Size = Histogram[i] / Scaler) > ImageWidth) Size = ImageWidth;
  234.         for (j = 0; j < Size; j++)
  235.         Line[j] = Color;
  236.         for (j = Size; j < ImageWidth; j++)
  237.         Line[j] = GifFileOut -> SBackGroundColor;
  238.  
  239.         /* Move to next color: */
  240.         if (++Color >= (1 << HISTO_BITS_PER_PIXEL)) Color = 1;
  241.  
  242.         /* Dump this histogram entry as many times as required: */
  243.         for (j = 0; j < ImageHeight / NumColors; j++) {
  244.         if (EGifPutLine(GifFileOut, Line, ImageWidth) == GIF_ERROR)
  245.             QuitGifError(GifFileIn, GifFileOut);
  246.         GifQprintf("\b\b\b\b%-4d", Count--);
  247.         }
  248.     }
  249.  
  250.     if (EGifCloseFile(GifFileOut) == GIF_ERROR)
  251.         QuitGifError(GifFileIn, GifFileOut);
  252.     }
  253. }
  254.  
  255. /******************************************************************************
  256. * Close both input and output file (if open), and exit.                  *
  257. ******************************************************************************/
  258. static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut)
  259. {
  260.     PrintGifError();
  261.     if (GifFileIn != NULL) DGifCloseFile(GifFileIn);
  262.     if (GifFileOut != NULL) EGifCloseFile(GifFileOut);
  263.     exit(1);
  264. }
  265.